﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Alvin.Common.Utils;

namespace WindowTest
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            SetGlassRegion();
            this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            //SetGlassRegion();
            var path = AppDomain.CurrentDomain.BaseDirectory;
            path = System.IO.Path.Combine(path, "Images");
            if (!System.IO.Directory.Exists(path))
            {
                System.IO.Directory.CreateDirectory(path);
            }
            for (var i = 32; i < 127; i++)
            {
                var c = (char) i;
                var s = new string(new char[]{c});
                var p = System.IO.Path.Combine(path, string.Format("{0}.bmp", i));
                using (var fs = new FileStream(p, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                {
                    var img = BarCodeUtil.Code39Full.GetImage(s, 200, 60, CommentPosition.TopLeft, null);
                    img.Save(fs,ImageFormat.Bmp);
                }
            }
            //6921734976581
            path = System.IO.Path.Combine(path, string.Format("{0}.bmp", "95270078"));
            using (var fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite))
            {
                var img = BarCodeUtil.CodeEan128.GetImage("95270078", 200, 60, CommentPosition.TopLeft, null);
                img.Save(fs, ImageFormat.Bmp);
            }
            this.Image.Source = new BitmapImage(new Uri(@"F:\WorkSpaces\Code\SRC\WindowTest\bin\Debug\image.bmp"));
            this.PreviewKeyDown += new KeyEventHandler(MainWindow_PreviewKeyDown);
        }

        void MainWindow_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            AppendText("MainWindow_PreviewKeyDown");
            switch (e.Key)
            {
                    case Key.A:
                    {
                        //TextBoxA.Focus();
                        //e.Handled = true;
                    }break;
                    case Key.B:
                    {
                        //TextBoxB.Focus();
                        //e.Handled = true;
                    }break;
                    case Key.C:
                    {
                        //TextBoxC.Focus();
                        //e.Handled = true;
                    }break;
            }
        }

        private void TextBoxA_OnPreviewKeyDown(object sender, KeyEventArgs e)
        {
            AppendText("TextBoxA_OnPreviewKeyDown");
        }

        private void TextBoxB_OnPreviewKeyDown(object sender, KeyEventArgs e)
        {
            AppendText("TextBoxB_OnPreviewKeyDown");
        }

        private void TextBoxC_OnPreviewKeyDown(object sender, KeyEventArgs e)
        {
            AppendText("TextBoxC_OnPreviewKeyDown");
        }

        private void TextBoxA_OnTextChanged(object sender, TextChangedEventArgs e)
        {
            AppendText("TextBoxA_OnTextChanged");
        }

        private void TextBoxB_OnTextChanged(object sender, TextChangedEventArgs e)
        {
            MessageBox.Show("TextBoxB_OnTextChanged");
        }

        private void TextBoxC_OnTextChanged(object sender, TextChangedEventArgs e)
        {
            AppendText("TextBoxC_OnTextChanged");
        }

        private void AppendText(string msg)
        {
            this.TextBoxMsg.AppendText(string.Format("{0}\n", msg));
        }

        [DllImport("dwmapi.dll", PreserveSig = false)]
        public static extern void DwmExtendFrameIntoClientArea
                        (IntPtr hwnd, ref Margins margins);

        [DllImport("dwmapi.dll", PreserveSig = false)]
        public static extern bool DwmIsCompositionEnabled();

        // defines how far we are extending the Glass margins
        private Margins margins;

        // uses PInvoke to setup the Glass area.
        private void SetGlassRegion()
        {
            // Set up the glass effect using padding as the defining glass region
            if (!DwmIsCompositionEnabled()) return;
            margins = new Margins();
            margins.Top = Padding.Top;
            margins.Left = Padding.Left;
            margins.Bottom = Padding.Bottom;
            margins.Right = Padding.Right;
            var hwndSource = PresentationSource.FromVisual(this) as HwndSource;
            if (hwndSource == null) return;
            DwmExtendFrameIntoClientArea(hwndSource.Handle, ref margins);
        }

        private void CloseWindowCommandExcuted(object sender, ExecutedRoutedEventArgs e)
        {
            var window = Window.GetWindow(this);
            Microsoft.Windows.Shell.SystemCommands.CloseWindow(window);//不明白为什么这么做
        }

        private void TextBoxA_OnGotFocus(object sender, RoutedEventArgs e)
        {
            var tx = sender as TextBox;
            if(tx == null) return;
            var board = new Alvin.Window.Utils.KeyBoardWindow(this);
            board.Show();
            tx.DataContext = board;
        }

        private void TextBoxA_OnLostFocus(object sender, RoutedEventArgs e)
        {
            //var tx = sender as TextBox;
            //if (tx == null) return;
            //var board = tx.DataContext as Alvin.Window.Utils.KeyBoardWindow;
            //if (board == null)
            //{
            //    return;
            //}
            //board.Close();
        }

        private void BtnNewWindow(object sender, RoutedEventArgs e)
        {
            new MainWindow().ShowDialog();
        }
    }
    [StructLayout(LayoutKind.Sequential)]
    public struct Margins
    {
        public double Left;
        public double Right;
        public double Top;
        public double Bottom;
    }


}
